package com.zj.processors.platform;

import com.zj.bean.TableInfo;
import com.zj.job.CommonJob;
import com.zj.processors.BaseCommonProcessor;
import com.zj.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.StringWriter;
import java.util.Date;

@Component
public class DubboServiceProcessor extends BaseCommonProcessor {


    @Override
    public String getName() {
        return "dubboService";
    }

    @Override
    public String process(TableInfo tableInfo, CommonJob commonJob) {
        init(tableInfo, commonJob);
        try {
            createFiles();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }

    private String getPackageName(String tableName) {

        if(tableName.contains("_")){
            tableName = tableName.substring(tableName.indexOf("_"), tableName.length());
        }
        tableName=tableName.replaceAll("_", "");
        return tableName.toLowerCase();
    }

    private void makedir(String dirPath) {
        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdir();
        }
    }
    public String createFiles() throws Exception {

        String servicePackage=getParam("servicePackage");


        String serverPath = getParam("baseDir") + "zj-"+servicePackage+"-server/src/main/java/com/zj/" +servicePackage ;
        makedir(serverPath);

        String clientPath = getParam("baseDir") + "zj-"+servicePackage+"-client/src/main/java/com/zj/" +servicePackage ;
        makedir(clientPath);
        VelocityContext context = new VelocityContext();
        String modelName=convertDomainName(tableInfo.getTableName());
        context.put("modelName", modelName);
        context.put("modelProperty", lowFirstChar(modelName));
        context.put("servicePackage", getParam("servicePackage"));
        String serviceName=getParam("serviceName");
        if(StringUtils.isEmpty(serviceName)){
            serviceName=convertServiceName(this.tableInfo.getTableName());
        }
        log("use serviceName: " + serviceName);

        context.put("serviceName", serviceName);

        File queryFile = new File(clientPath + "/bean/" + modelName + "Query.java");
        if(!queryFile.exists()){
            writeFile(context,"platform-vm/query.vm",queryFile);
            log("new queryFile: " + queryFile.getName());
        }else{
            log(queryFile.getName() + "已存在========打印内容");
            StringWriter stringWriter = new StringWriter();
            Template vmTemplate = loadTemplate("platform-vm/query.vm");
            vmTemplate.merge(context,stringWriter);
            log(stringWriter.toString());
        }
        File serviceFile = new File(clientPath + "/service/" + serviceName + "Service.java");
        if (!serviceFile.exists()) {
            writeFile(context,"platform-vm/dubboService.vm",serviceFile);
            log("new serviceFile: " + serviceFile.getName());
        }else{
            log(serviceFile.getName() + "已存在========打印内容");
            StringWriter stringWriter = new StringWriter();
            Template vmTemplate = loadTemplate("platform-vm/dubboService.vm");
            vmTemplate.merge(context,stringWriter);
            log(stringWriter.toString());
        }
        File serviceImplFile = new File(serverPath + "/service/impl/" + serviceName + "ServiceImpl.java");
        if (!serviceImplFile.exists()) {
            writeFile(context,"platform-vm/dubboServiceImpl.vm",serviceImplFile);
            log("new serviceImplFile: " + serviceImplFile.getName());
        }else{
            log(serviceImplFile.getName() + "已存在========打印内容");
            StringWriter stringWriter = new StringWriter();
            Template vmTemplate = loadTemplate("platform-vm/dubboServiceImpl.vm");
            vmTemplate.merge(context,stringWriter);
            log(stringWriter.toString());
        }

        commonJob.log("task ok");
        return "ok";
    }

}
